﻿using UnityEngine;
using System.Collections;
namespace SGUI
{
    public class TabControl : MonoBehaviour
    {

        public Transform TabBars;
        public Transform TabContents;
        public event System.Action<string> SelectItemChanged;
        public int SelectIndex { get; private set; }
        void Start()  
        {
            if (this.TabBars.childCount > 0)
            {
                this.SelectIndex = 0;
            }
            for (int i = 0; i < this.TabBars.childCount;i++ )
            {
                UIEventListener.Get(this.TabBars.GetChild(i).gameObject).onClick += this.OnTabClick;
                this.TabContents.GetChild(i).gameObject.SetActive(i == this.SelectIndex);
                
            }
           
        }
        public void OnTabClick(GameObject go)
        {
            for (int i = 0; i < this.TabBars.transform.childCount;i++)
            {
                if (this.TabBars.GetChild(i).name == go.name)
                {
                    this.SelectIndex = i;
                    this.TabContents.GetChild(i).gameObject.SetActive(true);
                    if (this.SelectItemChanged != null)
                    {
                        this.SelectItemChanged(this.TabBars.GetChild(i).name);
                    }
                }
                else 
                {
                    this.TabContents.GetChild(i).gameObject.SetActive(false);
                }
            }
        }
    }
}
